package com.sgb.jicai.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.jicai.BR;
import com.sgb.jicai.R;
import com.swgk.core.util.TimeUtil;
import com.sgb.jicai.entity.ShoplistBean;
import com.sgb.link.utils.NumericalTransformationTool;
import com.sgb.jicai.weight.SelectInputView;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Time:2021/8/11
 * AUTHOR:F_Q
 * Description:
 */
public class DetailedAdapter extends BaseRecycleAdapter<ShoplistBean> {
    private Calendar dayCalendar = Calendar.getInstance();

    public DetailedAdapter(Context context, List<ShoplistBean> list) {
        super(context, R.layout.item_jc_detailed, list);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ShoplistBean shoplistBean = datas.get(position);
        ItemInputTextView buy_num = holder.itemView.findViewById(R.id.buy_num);//采购量
        ItemInputTextView pick_time = holder.itemView.findViewById(R.id.pick_time);//提货时间
        ItemInputTextView approach_time = holder.itemView.findViewById(R.id.approach_time);//进场时间
        ItemInputTextView end_time = holder.itemView.findViewById(R.id.end_time);//结束时间
        SelectInputView material_goods = holder.itemView.findViewById(R.id.material_goods);//商品材料
        SelectInputView quality_standard = holder.itemView.findViewById(R.id.quality_standard);//质量标准
        SelectInputView maintenance = holder.itemView.findViewById(R.id.maintenance);//维修保养
        SelectInputView use_goods = holder.itemView.findViewById(R.id.use_goods);//商品用途
        SelectInputView signalment = holder.itemView.findViewById(R.id.signalment);//特征描述
        RecyclerView recyclerview = holder.itemView.findViewById(R.id.recyclerview);
        RelativeLayout delete = holder.itemView.findViewById(R.id.delete);//删除

        editTextChanged(material_goods.getContent_edt(), datas.get(position), position, 2);
        editTextChanged(quality_standard.getContent_edt(), datas.get(position), position, 3);
        editTextChanged(maintenance.getContent_edt(), datas.get(position), position, 4);
        editTextChanged(use_goods.getContent_edt(), datas.get(position), position, 5);
        editTextChanged(signalment.getContent_edt(), datas.get(position), position, 6);
        //时间处理
        if (TextUtils.equals(datas.get(position).getGroupbuyClass(), "1")) {
            if (!TextUtils.isEmpty(datas.get(position).getPickTime())) {
                pick_time.setContent(DateUtils.timestampToDate(Long.parseLong(datas.get(position).getPickTime())));
            } else {
                pick_time.setContent("");
            }
        } else {
            if (!TextUtils.isEmpty(datas.get(position).getEntranceTime())) {
                approach_time.setContent(DateUtils.timestampToDate(Long.parseLong(datas.get(position).getEntranceTime())));
            } else {
                approach_time.setContent("");
            }
            if (!TextUtils.isEmpty(datas.get(position).getExitTime())) {
                end_time.setContent(DateUtils.timestampToDate(Long.parseLong(datas.get(position).getExitTime())));
            } else {
                end_time.setContent("");
            }
        }
      /*  if (position > 0) {
            delete.setVisibility(View.VISIBLE);
        } else {
            delete.setVisibility(View.GONE);
        }*/
        if (datas.size()>1){
            delete.setVisibility(View.VISIBLE);
        }else {
            delete.setVisibility(View.GONE);
        }
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDelete.Delete(position);
            }
        });
        //集采采购起始量
        NumericalTransformationTool.OnItemInputTextViewChange(buy_num);
        buy_num.setInfilterType(ItemInputTextView.TYPE_NUM);//纯数字
        editTextChanged(buy_num.getContent_edt(), datas.get(position), position, 1);

        //提货时间
        pick_time.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                pickTimeData(1, pick_time, datas.get(position));
            }
        });
        //进场时间
        approach_time.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                pickTimeData(2, approach_time, datas.get(position));
            }
        });
        //结束时间
        end_time.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {//setEntranceTime
                if (!TextUtils.isEmpty(datas.get(position).getEntranceTime())){
                    pickTimeData(3, end_time, datas.get(position));
                }else {
                    MToast.showToast(context, "请先选择进场时间");
                }
            }
        });
        //二级adapter处理
        if (shoplistBean.getAttrs() != null && shoplistBean.getAttrs().size() > 0) {
            recyclerview.setVisibility(View.VISIBLE);
            adapterData(holder, shoplistBean);
        } else {
            recyclerview.setVisibility(View.GONE);
        }


    }


    @Override
    public void convert(ItemViewHolder holder, ShoplistBean entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    private void adapterData(ItemViewHolder holder, ShoplistBean entity) {
        RecyclerView recyclerview = holder.itemView.findViewById(R.id.recyclerview);
        DetailedTwoAdapter adapter = new DetailedTwoAdapter(context, entity.getAttrs());
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        recyclerview.setAdapter(adapter);
    }

    /**
     * 时间选择
     *
     * @param pick_time
     * @param shoplistBean
     */
    private void pickTimeData(int type, ItemInputTextView pick_time, ShoplistBean shoplistBean) {
        if (TextUtils.isEmpty(shoplistBean.getStartTime())) {
            dayCalendar.setTime(new Date());
        } else {
            dayCalendar.setTime(TimeUtil.getDateFromFormatString(TimeUtil.getDateStringYMD(Long.valueOf(shoplistBean.getStartTime()))));//非商品的入场时间和结束时间需在报价截止时间后
        }
        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);
        TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            switch (type) {
                case 1:
                    shoplistBean.setPickTime(String.valueOf(DateUtils.dateTo13Timestamp(format)));
                    break;
                case 2:
                    shoplistBean.setEntranceTime(String.valueOf(DateUtils.dateTo13Timestamp(format)));
                    break;
                case 3:
                    if (Long.parseLong(shoplistBean.getEntranceTime()) < Long.parseLong(String.valueOf(DateUtils.dateTo13Timestamp(format)))) {
                        shoplistBean.setExitTime(String.valueOf(DateUtils.dateTo13Timestamp(format)));
                    } else {
                        MToast.showToast(context, "结束日期必须大于进场日期");
                        return;
                    }
                    break;
            }
            pick_time.setContent(format);
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
                .build();
        timePicker.show();
    }

    private onDelete onDelete;

    public void setOnDelete(onDelete onDelete) {
        this.onDelete = onDelete;
    }

    public interface onDelete {
        void Delete(int pos);
    }

    private void editTextChanged(EditText editText, ShoplistBean field, int pos, int type) {
        if (editText.getTag() != null) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
            editText.clearFocus();
        }
        switch (type) {
            case 1:
                editText.setText(field.getNum());//采购量
                break;
            case 2:
                editText.setText(field.getGoodsMaterial() + "");//商品材料
                break;
            case 3:
                editText.setText(field.getGoodsQuality() + "");//质量标准
                break;
            case 4:
                editText.setText(field.getGoodsMaintenance() + "");//维修保养
                break;
            case 5:
                editText.setText(field.getGoodsPurpose() + "");//商品用途
                break;
            case 6:
                editText.setText(field.getGoodsDescribe() + "");//特征描述
                break;
        }

        TextWatcher watcher = new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                switch (type) {
                    case 1:
                        getDatas().get(pos).setNum(s.toString());//采购量
                        break;
                    case 2:
                        getDatas().get(pos).setGoodsMaterial(s.toString() + "");//商品材料
                        break;
                    case 3:
                        getDatas().get(pos).setGoodsQuality(s.toString() + "");//质量标准
                        break;
                    case 4:
                        getDatas().get(pos).setGoodsMaintenance(s.toString() + "");//维修保养
                        break;
                    case 5:
                        getDatas().get(pos).setGoodsPurpose(s.toString() + "");//商品用途
                        break;
                    case 6:
                        getDatas().get(pos).setGoodsDescribe(s.toString() + "");//特征描述
                        break;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
